package com.mayisoftware.mayioa.business.po.impl.controller;

import com.mayisoftware.mayioa.business.po.api.domain.POBasicInfo;
import com.mayisoftware.mayioa.business.po.api.domain.POSearchInfo;
import com.mayisoftware.mayioa.business.po.api.service.IPOService;
import com.mayisoftware.mayioa.common.api.annotation.Log;
import com.mayisoftware.mayioa.common.api.base.AjaxResult;
import com.mayisoftware.mayioa.common.api.constant.Constants;
import com.mayisoftware.mayioa.common.api.enums.BusinessType;
import com.mayisoftware.mayioa.common.impl.utils.ExcelUtil;
import com.mayisoftware.mayioa.platform.api.domain.SysUser;
import com.mayisoftware.mayioa.platform.api.service.ISysDictDataService;
import com.mayisoftware.mayioa.platform.impl.util.ShiroUtils;
import com.mayisoftware.mayioa.platform.impl.web.base.BaseController;
import com.mayisoftware.mayioa.platform.impl.web.page.TableDataInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/po/board")
public class POController extends BaseController{
    private static final Logger log = LoggerFactory.getLogger(POController.class);

    @Autowired
    private IPOService poService;

    @Autowired
    ISysDictDataService sysDictDataService;

    private String prefix = "po/board";

    @RequiresPermissions("po:board:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/po";
    }


    @RequiresPermissions("po:board:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(POSearchInfo searchInfo)
    {
        startPage();
        List<POBasicInfo> poBasicInfos = new ArrayList<>();
        poBasicInfos = poService.queryPOInfos(searchInfo);
        return getDataTable(poBasicInfos);
    }


    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        return prefix + "/add";
    }

    @Log(title = "添加PO", businessType = BusinessType.INSERT)
    @RequiresPermissions("po:board:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(POBasicInfo poBasicInfo)
    {
        poBasicInfo.setUpdateBy(ShiroUtils.getLoginName());
        int s = poService.insertPoInfo(poBasicInfo);
        if(s==-1){
            return error("添加失败！");
        }
        return toAjax(s);
    }

    @RequiresPermissions("po:board:remove")
    @Log(title = "删除PO", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(poService.deletePos(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }



    /**
     * 修改角色
     */
    @GetMapping("/edit/{poId}")
    public String edit(@PathVariable("poId") String poId, ModelMap mmap)
    {
        mmap.put("poInfo",poService.getPoInfo(poId));
        return prefix + "/edit";
    }

    /**
     * 修改保存角色
     */
    @RequiresPermissions("po:board:edit")
    @Log(title = "PO修改", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult editSave(POBasicInfo poBasicInfo)
    {
        poBasicInfo.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(poService.updatePoInfo(poBasicInfo));
    }


    @Log(title = "导出PO信息", businessType = BusinessType.EXPORT)
    @RequiresPermissions("po:board:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(POSearchInfo poSearchInfo)
    {
        List<POBasicInfo> poBasicInfos = poService.queryPOInfos(poSearchInfo);
        formatPoolFigures(poBasicInfos);
        ExcelUtil<POBasicInfo> util = new ExcelUtil<POBasicInfo>(POBasicInfo.class);
        return util.exportExcel(poBasicInfos, "PO信息");
    }

    private void formatPoolFigures(List<POBasicInfo> poBasicInfos) {
        if (CollectionUtils.isEmpty(poBasicInfos)){
            return;
        }

        for (POBasicInfo poBasicInfo:poBasicInfos){
            if(null != poBasicInfo.getPrjType()){
                poBasicInfo.setPrjType(sysDictDataService.selectDictLabel("po_type",poBasicInfo.getPrjType()));
            }

            if(null != poBasicInfo.getPdu()){
                poBasicInfo.setPdu(sysDictDataService.selectDictLabel("human_pdu",poBasicInfo.getPdu()));
            }
            if (null !=poBasicInfo.getRegion() ){
                poBasicInfo.setRegion(sysDictDataService.selectDictLabel("check_region",poBasicInfo.getRegion()));
            }

            if (null != poBasicInfo.getStatus()){
                poBasicInfo.setStatus(sysDictDataService.selectDictLabel("po_status",poBasicInfo.getStatus()));
            }

            if (null != poBasicInfo.getNewPORisk()){
                poBasicInfo.setNewPORisk(sysDictDataService.selectDictLabel("risk_level",poBasicInfo.getNewPORisk()));
            }

        }
    }
}
